﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;

namespace Base64Img
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        #region IMG2TEXT

        //图片 转为    base64编码的文本
        private void btnOpenImg_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Title = "选择要转换的图片";
            dlg.Filter = "Image files (*.jpg;*.bmp;*.gif;*.png)|*.jpg;*.jpeg;*.gif;*.bmp;*.png|AllFiles (*.*)|*.*";
            if (DialogResult.OK == dlg.ShowDialog())
            {
                ImgToBase64String(dlg.FileName);
            }
        }
        //图片 转为    base64编码的文本
        private void ImgToBase64String(string Imagefilename)
        {
            try
            {
                Bitmap bmp = new Bitmap(Imagefilename);
                this.pictureBox1.Refresh();
                this.pictureBox1.Image = bmp;
                if ((bmp.Width > this.pictureBox1.Width) || (bmp.Height > pictureBox1.Height))
                {
                    this.pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
                }
                else
                {
                    this.pictureBox1.SizeMode = PictureBoxSizeMode.CenterImage;
                }
                FileStream fs = new FileStream(Imagefilename + ".txt", FileMode.Create);

                MemoryStream ms = new MemoryStream();
                try
                {
                    if (Imagefilename.ToUpper().EndsWith("BMP"))
                    {
                        bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
                    }
                    else if (Imagefilename.ToUpper().EndsWith("GIF"))
                    {
                        bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
                    }
                    else if (Imagefilename.ToUpper().EndsWith("PNG"))
                    {
                        bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                    }
                    else if (Imagefilename.ToUpper().EndsWith("EMF"))
                    {
                        bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Emf);
                    }
                    else if (Imagefilename.ToUpper().EndsWith("EXIF"))
                    {
                        bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Exif);
                    }
                    else if (Imagefilename.ToUpper().EndsWith("ICO"))
                    {
                        bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Icon);
                    }
                    else if (Imagefilename.ToUpper().EndsWith("TIFF"))
                    {
                        bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Tiff);
                    }
                    else if (Imagefilename.ToUpper().EndsWith("WMF"))
                    {
                        bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Wmf);
                    }
                    else
                    {
                        bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                    }

                    byte[] arr = new byte[ms.Length];
                    ms.Position = 0;
                    ms.Read(arr, 0, (int)ms.Length);
                    String strbaser64 = Convert.ToBase64String(arr);
                    this.rtbBase64.Text = strbaser64;
                }
                catch (Exception ex)
                {
                    MessageBox.Show("ImgToBase64String 转换失败\nException:" + ex.Message);
                }
                finally
                {
                    ms.Close();
                    fs.Close();
                }

                MessageBox.Show("转换成功!");
            }
            catch (Exception ex)
            {
                MessageBox.Show("ImgToBase64String 转换失败\nException:" + ex.Message);
            }
        }

        private void ToolStripMenuItem_SaveAsBase64_Click(object sender, EventArgs e)
        {
            try
            {
                SaveFileDialog sBox = new SaveFileDialog();
                sBox.Filter = "txt files|*.txt";
                if (DialogResult.OK == sBox.ShowDialog())
                {
                    StreamWriter sw = new StreamWriter(sBox.FileName, false, Encoding.Default);
                    sw.Write(this.rtbBase64.Text.Trim());
                    sw.Close();
                    sw.Dispose();

                    MessageBox.Show("文本已保存");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        #endregion

        #region TEXT2IMG

        //base64编码的文本转为图片
        private void BtnOpenTxt_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Title = "选择要转换的base64编码的文本";
            dlg.Filter = "txt files|*.txt";
            if (DialogResult.OK == dlg.ShowDialog())
            {
                Base64StringToImage(dlg.FileName);
            }
        }
        //base64编码的文本转为图片
        private void Base64StringToImage(string txtFileName)
        {
            FileStream ifs = null;
            StreamReader sr = null;
            MemoryStream ms = null;

            try
            {
                ifs = new FileStream(txtFileName, FileMode.Open, FileAccess.Read);
                sr = new StreamReader(ifs);

                String inputStr = sr.ReadToEnd();

                this.rtbBase64.Text = inputStr;

                byte[] arr = Convert.FromBase64String(inputStr);
                ms = new MemoryStream(arr);
                Bitmap bmp = new Bitmap(ms);
                this.pictureBox1.Image = bmp;
                if ((bmp.Width > this.pictureBox1.Width) || (bmp.Height > pictureBox1.Height))
                {
                    this.pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
                }
                else
                {
                    this.pictureBox1.SizeMode = PictureBoxSizeMode.CenterImage;
                }

                if (bmp.RawFormat == System.Drawing.Imaging.ImageFormat.Bmp)
                {
                    bmp.Save(txtFileName + ".bmp", System.Drawing.Imaging.ImageFormat.Bmp);
                }
                else if (bmp.RawFormat == System.Drawing.Imaging.ImageFormat.Emf)
                {
                    bmp.Save(txtFileName + ".Emf", System.Drawing.Imaging.ImageFormat.Emf);
                }
                else if (bmp.RawFormat == System.Drawing.Imaging.ImageFormat.Exif)
                {
                    bmp.Save(txtFileName + ".Exif", System.Drawing.Imaging.ImageFormat.Exif);
                }
                else if (bmp.RawFormat == System.Drawing.Imaging.ImageFormat.Gif)
                {
                    bmp.Save(txtFileName + ".Gif", System.Drawing.Imaging.ImageFormat.Gif);
                }
                else if (bmp.RawFormat == System.Drawing.Imaging.ImageFormat.Png)
                {
                    bmp.Save(txtFileName + ".Png", System.Drawing.Imaging.ImageFormat.Png);
                }
                else if (bmp.RawFormat == System.Drawing.Imaging.ImageFormat.Icon)
                {
                    bmp.Save(txtFileName + ".ico", System.Drawing.Imaging.ImageFormat.Icon);
                }
                else if (bmp.RawFormat == System.Drawing.Imaging.ImageFormat.Tiff)
                {
                    bmp.Save(txtFileName + ".Tiff", System.Drawing.Imaging.ImageFormat.Tiff);
                }
                else if (bmp.RawFormat == System.Drawing.Imaging.ImageFormat.Wmf)
                {
                    bmp.Save(txtFileName + ".Wmf", System.Drawing.Imaging.ImageFormat.Wmf);
                }
                else if (bmp.RawFormat == System.Drawing.Imaging.ImageFormat.Tiff)
                {
                    bmp.Save(txtFileName + ".Tiff", System.Drawing.Imaging.ImageFormat.Tiff);
                }
                else
                {
                    bmp.Save(txtFileName + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
                }
                MessageBox.Show("转换成功！");
            }
            catch (Exception ex)
            {
                MessageBox.Show("Base64StringToImage 转换失败\nException：" + ex.Message);
            }
            finally
            {
               // ms.Close();
                sr.Close();
                ifs.Close();
            }
        }

        private void btnToImg_Click(object sender, EventArgs e)
        {
            MemoryStream ms = null;
            try
            {
                byte[] arr = Convert.FromBase64String(this.rtbBase64.Text);
                ms = new MemoryStream(arr);
                Bitmap bmp = new Bitmap(ms);
                this.pictureBox1.Refresh();
                this.pictureBox1.Image = bmp;
                if ((bmp.Width > this.pictureBox1.Width) || (bmp.Height > pictureBox1.Height))
                {
                    this.pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
                }
                else
                {
                    this.pictureBox1.SizeMode = PictureBoxSizeMode.CenterImage;
                }
                //bmp.Dispose();
                MessageBox.Show("转换成功！");
            }
            catch (Exception ex)
            {
                MessageBox.Show("Base64StringToImage 转换失败\nException：" + ex.Message);
            }
            finally
            {
               // ms.Close();
            }
        }

        private void ToolStripMenuItem_SaveAsImg_Click(object sender, EventArgs e)
        {
            try
            {
                SaveFileDialog sBox = new SaveFileDialog();
                sBox.Filter = "Image files (*.jpg;*.bmp;*.gif;*.png)|*.jpg;*.jpeg;*.gif;*.bmp;*.png|AllFiles (*.*)|*.*";
                sBox.AddExtension = false;
                if (DialogResult.OK == sBox.ShowDialog())
                {
                    Bitmap bmp = new Bitmap(this.pictureBox1.Image);

                    if (bmp.RawFormat == System.Drawing.Imaging.ImageFormat.Bmp)
                    {
                        bmp.Save(sBox.FileName + ".bmp", System.Drawing.Imaging.ImageFormat.Bmp);
                    }
                    else if (bmp.RawFormat == System.Drawing.Imaging.ImageFormat.Emf)
                    {
                        bmp.Save(sBox.FileName + ".Emf", System.Drawing.Imaging.ImageFormat.Emf);
                    }
                    else if (bmp.RawFormat == System.Drawing.Imaging.ImageFormat.Exif)
                    {
                        bmp.Save(sBox.FileName + ".Exif", System.Drawing.Imaging.ImageFormat.Exif);
                    }
                    else if (bmp.RawFormat == System.Drawing.Imaging.ImageFormat.Gif)
                    {
                        bmp.Save(sBox.FileName + ".Gif", System.Drawing.Imaging.ImageFormat.Gif);
                    }
                    else if (bmp.RawFormat == System.Drawing.Imaging.ImageFormat.Png)
                    {
                        bmp.Save(sBox.FileName + ".Png", System.Drawing.Imaging.ImageFormat.Png);
                    }
                    else if (bmp.RawFormat == System.Drawing.Imaging.ImageFormat.Icon)
                    {
                        bmp.Save(sBox.FileName + ".ico", System.Drawing.Imaging.ImageFormat.Icon);
                    }
                    else if (bmp.RawFormat == System.Drawing.Imaging.ImageFormat.Tiff)
                    {
                        bmp.Save(sBox.FileName + ".Tiff", System.Drawing.Imaging.ImageFormat.Tiff);
                    }
                    else if (bmp.RawFormat == System.Drawing.Imaging.ImageFormat.Wmf)
                    {
                        bmp.Save(sBox.FileName + ".Wmf", System.Drawing.Imaging.ImageFormat.Wmf);
                    }
                    else if (bmp.RawFormat == System.Drawing.Imaging.ImageFormat.Tiff)
                    {
                        bmp.Save(sBox.FileName + ".Tiff", System.Drawing.Imaging.ImageFormat.Tiff);
                    }
                    else
                    {
                        bmp.Save(sBox.FileName + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
                    }

                    MessageBox.Show("图片已保存");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        #endregion
    }
}
